package com.ncloud.resman.controller;

import com.alibaba.fastjson.JSONObject;
import com.ncloud.common.body.Result;
import com.ncloud.common.exception.UserException;
import com.ncloud.common.utils.ResultUtil;
import com.ncloud.resman.busi.DeviceLocationBusi;
import com.ncloud.resman.entity.DeviceLocation;
import com.ncloud.resman.service.DeviceLocationService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * Created by shiqiang on 2017/8/15.
 */
@Api(value = "自动化运维设备管理", description = "设备位置信息", tags = "设备位置信息",
        basePath="/",consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/resman/device")
public class DeviceLocationController {
    @Autowired
    private DeviceLocationBusi deviceLocationBusi;
    @Autowired
    private DeviceLocationService deviceLocationService;
    @GetMapping(value = "/location/{eqptType}/{state}")
    public Result<?> getLocationList(@RequestParam(required=false) Long pageSize, @RequestParam(required=false) Long pageNum,
                                  @RequestParam(required=false) Long deviceId,@PathVariable("eqptType") String eqptType,
                                     @PathVariable("state") String state){
        JSONObject json = new JSONObject();
        if(pageSize != null && pageNum != null){
            json.put("pageSize", pageSize);
            json.put("pageNum", pageNum);
        }
        if(deviceId!=null){
            json.put("deviceId",deviceId);
        }
        json.put("eqptType",eqptType);
        json.put("state",state);
        return ResultUtil.success( deviceLocationBusi.getLocationList(json));
    }
    /*设备详情 ,统一调用 deviceMap中的设备查看*/
    @GetMapping(value = "/location/{deviceId}")
    public Result<JSONObject> getDeviceDetail(@PathVariable("deviceId") Long deviceId){
        return ResultUtil.success( deviceLocationBusi.getDeviceInfo(deviceId));
    }
    /*设备位置更新和新增（同修改）*/
    @PutMapping(value = "/location")
    public Result<Object> modifyLocationDevice(@RequestBody JSONObject json,
                                           HttpServletRequest request) throws Exception {
        Object userID = request.getAttribute("userID");
        if(StringUtils.isEmpty(userID)){
            throw new UserException(2001,"用户ID不存在！");
        }else{
            json.put("userID",Long.parseLong(userID.toString()));
        }
        deviceLocationBusi.modifyDeviceLocation(json);
        return ResultUtil.success();
    }

    /*查询机柜u位占用情况*/
    @GetMapping(value = "/location/use/{cabinetId}/{uLocation}")
    public Result getLocationUse(@PathVariable("cabinetId") Long cabinetId,@PathVariable("uLocation") Integer uLocation){
       int num = deviceLocationService.queryLocationUseCount(cabinetId,uLocation);
       return ResultUtil.success(num);
    }
    /*设备删除*/
    @DeleteMapping(value = "/deviceDel/{deviceId}")
    public Result deleteDevice(@PathVariable("deviceId") Long deviceId){
        deviceLocationBusi.deleteDeviceInfos(deviceId);
        return ResultUtil.success();
    }
    /*设备下架*/
    @GetMapping(value = "/location/shelvesDevice/{deviceId}/{eqptType}")
    public Result shelvesDevice(@PathVariable("deviceId") Long deviceId,@PathVariable("eqptType") String eqptType,HttpServletRequest request){
        DeviceLocation location = new DeviceLocation();
        Object userID = request.getAttribute("userID");
        if(StringUtils.isEmpty(userID)){
            throw new UserException(2001,"用户ID不存在！");
        }
      deviceLocationBusi.shelvesDevice(deviceId,eqptType,Long.parseLong(userID.toString()));
        return ResultUtil.success();
    }

}
